package com.xvyy.tingshu.album.api;

import com.xvyy.tingshu.album.service.TrackInfoService;
import com.xvyy.tingshu.common.login.annotation.TsLogin;
import com.xvyy.tingshu.common.result.Result;
import com.xvyy.tingshu.model.album.TrackInfo;
import com.xvyy.tingshu.query.album.TrackInfoQuery;
import com.xvyy.tingshu.vo.album.AlbumTrackListVo;
import com.xvyy.tingshu.vo.album.TrackInfoVo;
import com.xvyy.tingshu.vo.album.TrackListVo;
import com.xvyy.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    //http://192.168.217.1:8500/api/album/trackInfo/saveTrackInfo
    @TsLogin
    @Operation(summary = "保存声音")
    @PostMapping("/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    //http://192.168.217.1:8500/api/album/trackInfo/findUserTrackPage/1/10
    @TsLogin
    @Operation(summary = "分页查询用户曲目")
    @PostMapping("/findUserTrackPage/{pn}/{pz}")
    public Result findUserTrackPage(
            @PathVariable("pn") Integer pn,
            @PathVariable("pz") Integer pz,
            @RequestBody TrackInfoQuery trackInfoQuery) {

        IPage<TrackListVo> trackInfoVoIPage = new Page(pn, pz);
        trackInfoVoIPage = trackInfoService.findUserTrackPage(trackInfoVoIPage, trackInfoQuery);
        return Result.ok(trackInfoVoIPage);
    }

    //http://192.168.217.1:8500/api/album/trackInfo/getTrackInfo/51943
    @Operation(summary = "声音回显")
    @GetMapping("/getTrackInfo/{trackId}")
    public Result getTrackInfo(@PathVariable("trackId") Long trackId) {

        TrackInfo trackInfo = trackInfoService.getTrackInfo(trackId);
        return Result.ok(trackInfo);
    }

    //http://192.168.217.1:8500/api/album/trackInfo/updateTrackInfo/51943
    @TsLogin   // 校验用户是否登录
    @PutMapping("/updateTrackInfo/{trackId}")
    @Operation(summary = "修改声音")
    public Result updateTrackInfo(
            @PathVariable("trackId") Long trackId,
            @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfoByTrackId(trackId, trackInfoVo);
        return Result.ok();
    }

    //http://192.168.217.1:8500/api/album/trackInfo/removeTrackInfo/51944
    @TsLogin
    @DeleteMapping("/removeTrackInfo/{trackId}")
    @Operation(summary = "删除声音")
    public Result removeTrackInfo(@PathVariable("trackId") Long trackId) {
        trackInfoService.removeTrackInfoByTrackId(trackId);
        return Result.ok();
    }

    //http://192.168.217.1:8500/api/album/trackInfo/findAlbumTrackPage/751/1/10
    @GetMapping("/findAlbumTrackPage/{albumId}/{pn}/{pz}")
    @Operation(summary = "根据专辑id获取专辑下的所有声音分页列表")
    @TsLogin(required = false)//这里优化过注解
    public Result findAlbumTrackPage(
            @PathVariable("albumId") Long albumId,
            @PathVariable("pn") Long pn,
            @PathVariable("pz") Long pz) {
        IPage<AlbumTrackListVo> trackInfoVoIPage = new Page(pn, pz);
        trackInfoVoIPage = trackInfoService.findAlbumTrackPage(trackInfoVoIPage, albumId);
        return Result.ok(trackInfoVoIPage);
    }

    //http://192.168.217.1:8500/api/album/trackInfo/getTrackStatVo/51947
    @GetMapping("/getTrackStatVo/{trackId}")
    @Operation(summary = "根据声音id获取声音统计信息")
    public Result getTrackStatVo(@PathVariable("trackId") Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVoByTrackId(trackId);
        return Result.ok(trackStatVo);
    }

    //http://192.168.217.1:8500/api/album/trackInfo/findUserTrackPaidList/49166
    @GetMapping("/findUserTrackPaidList/{currentTrackId}")
    @Operation(summary = "分级展示声音列表")
    @TsLogin
    public Result findUserTrackPaidList(@PathVariable("currentTrackId") Long currentTrackId) {
        List<Map<String, Object>> trackPaidResultMap = trackInfoService.findUserTrackPaidList(currentTrackId);
        return Result.ok(trackPaidResultMap);
    }

}

